package com.yiwei.hdb.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.yiwei.hdb.R;
import com.yiwei.hdb.adapter.HelpCenterAdapter;
import com.yiwei.hdb.api.callback.HelpContract;
import com.yiwei.hdb.api.requestInfo.HelpRequest;
import com.yiwei.hdb.util.PreferenceUtils;
import com.yiwei.hdb.constant.Const;
import com.yiwei.hdb.event.OnHomeEventMsg;
import com.yiwei.hdb.interfaces.OnRecyclerViewListener;
import com.yiwei.hdb.model.ContentDetailModel;
import com.yiwei.hdb.model.HelpModel;
import com.yiwei.hdb.util.StatusBarCompat;
import com.yiwei.hdb.view.CircularProgress;
import com.yiwei.hdb.view.Dialog;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ˙帮助界面
 * Created by Master.Dong on 16/4/10.
 */
public class HelpCenterActivity extends BaseActivity implements View.OnClickListener, HelpContract.View {


    private List<ContentDetailModel> list = new ArrayList<ContentDetailModel>();
    private String type = "";
    private RecyclerView recyclerView;
    private HelpCenterAdapter adapter = null;
    private CircularProgress loading;
    private String mWeixinName;
    private String mPhoneNum;
    private HelpRequest api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.help_center_activity);
        baseInitView(this, "HelpCenterActivity", Const.ViewType.ACTIVITY);
        initView();
    }

    private void initView() {
        api = HelpRequest.create();
        api.attachView(this);
        setTitleText(getResources().getString(R.string.help_center));
        setBackIntent();

        loading = (CircularProgress) findViewById(R.id.progressBar);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mWeixinName = PreferenceUtils.getString(getApplicationContext(), Const.KEY.SERVICE_WEIXIN, "稻草时代");
        mPhoneNum = PreferenceUtils.getString(getApplicationContext(), Const.KEY.SERVICE_PHONE, "13827260730");


        if (list.size() > 0) {
            adapter = new HelpCenterAdapter(this, list);
            recyclerView.setAdapter(adapter);
            recyclerView.addItemDecoration(new StickyRecyclerHeadersDecoration(adapter));
            adapter.setOnRecyclerViewListener(new MyOnRecyclerViewListener());
        } else
            getHelp();


    }

    /**
     * 获取帮助信息
     */
    private void getHelp() {
        api.getHelp();
    }

    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (b) {
            if (loading != null) loading.setVisibility(View.VISIBLE);
        } else {
            if (loading != null) loading.setVisibility(View.GONE);
        }
    }

    /**
     * 设置数据
     */
    private void setData(HelpModel model) {
        try {
            list.clear();
            for (int i = 0; i < model.getData().size(); i++) {
                for (int j = 0; j < model.getData().get(i).getList().size(); j++) {
                    ContentDetailModel helpModel = new ContentDetailModel();
                    helpModel.setId(model.getData().get(i).getList().get(j).getId());
                    helpModel.setTitle(model.getData().get(i).getList().get(j).getTitle());
//                    helpModel.setType(model.getData().get(i).getTitle());
                    if (!type.equals(model.getData().get(i).getTitle())) {
                        type = model.getData().get(i).getTitle();
                        helpModel.setType(type);
                    }
                    list.add(helpModel);
                }
            }

            adapter = new HelpCenterAdapter(this, list);
            recyclerView.setAdapter(adapter);
            recyclerView.addItemDecoration(new StickyRecyclerHeadersDecoration(adapter));
            adapter.setOnRecyclerViewListener(new MyOnRecyclerViewListener());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getHelpSuccess(HelpModel model) {
        setData(model);
    }

    /**
     * 列表点击事件监听
     */
    class MyOnRecyclerViewListener implements OnRecyclerViewListener {

        @Override
        public void onItemClick(List<?> list, int position) {
            List<ContentDetailModel> l = (List<ContentDetailModel>) list;

            Intent intent = new Intent();
            intent.putExtra("id", l.get(position).getId());
            intent.putExtra("type", Const.FragmentType.HELP);
            intent.setClass(HelpCenterActivity.this, HelpDetailActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.right_enter, R.anim.left_out);
        }

        @Override
        public boolean onItemLongClick(List<?> list, int position) {
            return false;
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        }

    }

    private void showWeixinDialog() {
        Dialog.showDefaultDialog(this, getResources().getString(R.string.tip), getResources().getString(R.string.copy_dob_weixin) + "“" + mWeixinName + "”公众号-关注", getResources().getString(R.string.cancel), getResources().getString(R.string.focus), true, new Dialog.DialogDefaultClickListener() {
            @Override
            public void confirm() {
                // ComponentName（组件名称）是用来打开其他应用程序中的Activity或服务的
                Intent intent = new Intent();
                ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");// 报名该有activity

                intent.setAction(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(cmp);

                startActivity(intent);
            }

            @Override
            public void cancel() {

            }
        });
    }

    private void showPhoneDialog() {
        Dialog.showDefaultDialog(this, getResources().getString(R.string.tip), getResources().getString(R.string.call_dob_phone) + mPhoneNum, getResources().getString(R.string.cancel), getResources().getString(R.string.dial), true, new Dialog.DialogDefaultClickListener() {
            @Override
            public void confirm() {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mPhoneNum));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

            @Override
            public void cancel() {

            }
        });
    }

    private void sendBroadcast() {
        Map<String, String> map = new HashMap<>();
        map.put("type", Const.SendType.UPDATE);
        EventBus.getDefault().post(new OnHomeEventMsg(map));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        api = null;
        sendBroadcast();
    }

}
